<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('Vision');
Globals::requireTable('Visionnew');
Globals::requireTable('User');
Globals::requireTable('Order');
Globals::requireTable('Account');
Globals::requireTable('Vlevel');
Globals::requireTable('Vmoney');
Globals::requireTable('Product');
Globals::requireTable('Tryseeorder');
Globals::requireTable('Circle');
Globals::requireTable('Circleorder');
Globals::requireTable('Config');
Globals::requireTable('Advertising');

class IndexController extends Controller
{
    protected $VisionTable;
    protected $VisionnewTable;
	protected $UserTable;
	protected $OrderTable;
	protected $AccountTable;
	protected $VlevelTable;
	protected $VmoneyTable;
	protected $ProductTable;
	protected $TryseeOrderTable;
	protected $CircleTable;
	protected $CircleOrderTable;
	protected $ConfigTable;
	protected $AdvertisingTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->VisionTable = new VisionTable($config);
		$this->VisionnewTable = new VisionnewTable($config);
		$this->UserTable = new UserTable($config);
		$this->OrderTable = new OrderTable($config);
		$this->AccountTable = new AccountTable($config);
		$this->VlevelTable = new VlevelTable($config);
		$this->VmoneyTable = new VmoneyTable($config);
		$this->ProductTable = new ProductTable($config);
		$this->TryseeOrderTable = new TryseeorderTable($config);
		$this->CircleTable = new CircleTable($config);
		$this->CircleOrderTable = new CircleorderTable($config);
		$this->ConfigTable = new ConfigTable($config);
		$this->AdvertisingTable = new AdvertisingTable($config);
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	//版本更新
	public function visionAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','vision'=>'','v-money'=>'','v-type'=>'2','v-rank'=>'','u-type'=>'','an_versions'=>'1.0.3'));
	        exit();
	    }
	    
	    $vlevel = 0;
	    $data = $this->VlevelTable->listAllWithFields('level',array('uid'=>$uid));
	    foreach ($data as $k=>$v){
	        $vlevel += $v['level'];
	    }
	    if($vlevel <= 0){
	        echo json_encode(array('returncode'=>'-2','vision'=>'','v-money'=>'','v-type'=>'2','v-rank'=>'','u-type'=>'','an_versions'=>'1.0.3'));
	        exit();
	    }
	    
	    
	    $vmoney = 0;
	    $data = $this->VmoneyTable->listAllWithFields('money',array('uid'=>$uid));
	    foreach ($data as $k=>$v){
	        $vmoney += $v['money'];
	    }
	    if($vmoney < 0){
	        echo json_encode(array('returncode'=>'-3','vision'=>'','v-money'=>'','v-type'=>'2','v-rank'=>'','u-type'=>'','an_versions'=>'1.0.3'));
	        exit();
	    }
	    
	    
	    $vision = $this->VisionTable->getRow(array('status'=>1),'id DESC');
	    
	    $uidtoken = $this->getParam('uidtoken');
	    if(empty($uidtoken)){
	        $uidtoken = getUidToken($uid);
	    }
	    
	    echo json_encode(array('returncode'=>'0','vision'=>$vision['vision'],'v-money'=>$vmoney,'v-type'=>'2','v-rank'=>$vlevel,'u-type'=>$uidtoken,'an_versions'=>'1.0.3'));
	    exit();
	}
	
	public function visionnewAction(){
	    $uid = $this->getIntParam('uid');
	    $machine_type = $this->getIntParam('machine_type');
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','vision'=>'','v-money'=>'','v-type'=>'2','v-rank'=>'','u-type'=>'','an_versions'=>'1.0.3'));
	        exit();
	    }
	    
	    
	    $vlevel = 0;
	    $data = $this->VlevelTable->listAllWithFields('level',array('uid'=>$uid));
	    foreach ($data as $k=>$v){
	        $vlevel += $v['level'];
	    }
	    
	    
	    $vmoney = 0;
	    $data = $this->VmoneyTable->listAllWithFields('money',array('uid'=>$uid));
	    foreach ($data as $k=>$v){
	        $vmoney += $v['money'];
	    }
	    $vmoney = round($vmoney,2);
	    
	    $vision = $this->getParam('version');//未使用
	    $vision_now = $this->VisionnewTable->getRow(array('status'=>1,'machine_type'=>$machine_type,'vision'=>$vision));
	    $vision_last = $this->VisionnewTable->getRow(array('status'=>1,'machine_type'=>$machine_type),'id DESC');
	    
	    $uidtoken = getUidToken($uid);
	    
	    $constraint = 1;
	    if(isset($vision_now['id']) && $vision_now['id'] < $vision_last['id']){
	        $where = " status = 1 and machine_type = {$machine_type} and id > {$vision_now['id']} and powerful = 2 ";
	        $ish = $this->VisionnewTable->getRow($where);
	        if(isset($ish['id']) && $ish['id'] > 0){
	            $constraint = 2;
	        }
	    }elseif(isset($vision_now['id']) && $vision_now['id'] == $vision_last['id']){
	        $constraint = 0;
	    }else{
	        $constraint = 0;
	    }
	    
	    $vstart = $this->ConfigTable->getRow(array('`key`'=>'visionnewvtypestart'));
	    $vend = $this->ConfigTable->getRow(array('`key`'=>'visionnewvtypeend'));
	    if(date('H:i:s') > $vstart['value'] && date('H:i:s') < $vend['value']){
	        $v_type = $this->ConfigTable->getRow(array('`key`'=>'visionnewvtype'));
	    }else{
	        $v_type['value'] = 1;
	    }
	    
	    $generalizeicon = $this->ConfigTable->getRow(array('`key`'=>'generalizeicon'));
	    $generalizeicon = $generalizeicon['value'];
	    $generalizeurl = $this->ConfigTable->getRow(array('`key`'=>'generalizeurl'));
	    $generalizeurl = $generalizeurl['value'];
	    $generalizelocation = $this->ConfigTable->getRow(array('`key`'=>'generalizelocation'));
	    $generalizelocation = $generalizelocation['value'];
	    
	    echo json_encode(array('returncode'=>'0','vision'=>$vision_last['vision'],'v-type'=>(string)$v_type['value'],'u-type'=>$uidtoken,'v-money'=>(string)$vmoney,'v-rank'=>(string)$vlevel,'downloadlink'=>$vision_last['downloadurl'],'constraint'=>(string)$constraint,'updateexplain'=>$vision_last['remark'],'sunshinediscount'=>'1','generalizeicon'=>$generalizeicon,'generalizeurl'=>$generalizeurl,'generalizelocation'=>$generalizelocation));
	    exit();
	}
	
	public function advertisingAction(){
	    $uid = $this->getIntParam('uid');
	    $machine_type = $this->getIntParam('machine_type');
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'','data'=>array()));
	        exit();
	    }
	    
	    $vstart = $this->ConfigTable->getRow(array('`key`'=>'visionnewvtypestart'));
	    $vend = $this->ConfigTable->getRow(array('`key`'=>'visionnewvtypeend'));
	    if(date('H:i:s') > $vstart['value'] && date('H:i:s') < $vend['value']){
	        $v_type = $this->ConfigTable->getRow(array('`key`'=>'visionnewvtype'));
	    }else{
	        $v_type['value'] = 1;
	    }
	    
	    $re_data = array();
	    $data = $this->AdvertisingTable->listAll(array('status'=>1,'machine_type'=>$machine_type));
	    foreach ($data as $k=>$v){
	        $re_data[] = array(
	            'contenturl' => $v['contenturl'],
	            'contenttype' => $v['type'],
	            'openurl' => $v['openurl'],
	            'contentsize' => $v['contentsize'],
	            'duration' => $v['duration'],
	            'close' => $v['closeflag'],
	            'v-type' => $v_type['value']
	        );
	    }
	    
	    echo json_encode(array('returncode'=>'0','message'=>'请求成功','data'=>$re_data));
	    exit();
	}
	
	
	//检查账号订单状态（脚本）
	public function checkendtimeAction(){
	    $update_id = array();
	    
	    $account = $this->AccountTable->listAll(array('status'=>2));
	    foreach ($account as $k=>$v){
	        if($v['endtime'] < time()){
	            $tmp_product = $this->ProductTable->getRow($v['pid']);
	            
	            $user = $this->UserTable->getRow($v['uid']);
	            $orders = $this->OrderTable->listAll(array('aid'=>$v['id'],'status'=>1));
	            foreach ($orders as $kk=>$vv){
	                $this->OrderTable->update(array('status'=>2),$vv['id']);
	                
	                $exreas = array(
	                    'pushtype' => 8,
	                    'content' => '您购买的（'.$tmp_product['name'].'）账号已到期'
	                );
	                do_jpush($vv['uid'],'小主会员服务到期了',$exreas);
	            }
	            
	            $exreas = array(
	                'pushtype' => 9,
	                'content' => '您提供的（'.$tmp_product['name'].'）账号已到期，请尽快修改提交又可以赚钱了！'
	            );
	            do_jpush($v['uid'],'小主提供的会员服务到期了',$exreas);
	            
	            $this->AccountTable->update(array('status'=>3),$v['id']);
	        }
	    }
	    
	    exit();
	}
	
	//检查试看视频订单状态（脚本）
	public function checktryseeendtimeAction(){
	    $update_id = array();
	    
	    $account = $this->TryseeOrderTable->listAll(array('status'=>2));
	    foreach ($account as $k=>$v){
	        if($v['endtime'] < time()){
	            $this->TryseeOrderTable->update(array('status'=>3),$v['id']);
	        }
	    }
	    
	    exit();
	}
	
	public function checkcircle1Action(){
	    $date = date('Y-m-d');
	    $data = $this->CircleTable->listAll(array('update_date'=>$date,'is_order'=>0));
	    foreach ($data as $v){
	        echo $v['id'].'<br />';
            $content = mb_substr($v['content'], 0, 90);
            $exreas = array(
                'pushtype' => 17,
                'content' => "您发布的圈子：{$content}\n担保期已到期，可进入我的圈子里修改。",
                'customfield' => 'http://mp.weixin.qq.com/s/***ACMomentsViewController***'
            );
            do_jpush($v['uid'],'您发布的圈子商品担保期到期通知',$exreas);
	    }
	    
	    exit();
	}
	
	public function checkcircle2Action(){
	    $date = date('Y-m-d');
	    $data = $this->CircleTable->listAll(array('fixation_end_date'=>$date,'is_order'=>1));
	    foreach ($data as $v){
	        echo $v['id'].'<br />';
            $content = mb_substr($v['content'], 0,90);
            $exreas = array(
                'pushtype' => 17,
                'content' => "您购买的圈子：{$content}\n担保期已到期。",
                'customfield' => 'http://mp.weixin.qq.com/s/***ACMomentsViewController***'
                    );
            do_jpush($v['uid'],'您购买的圈子商品担保期到期通知',$exreas);
	    }
	    
	    exit();
	}
	
	public function test2Action(){
	    echo date('Y-m-d H');
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('IndexController', 'Controller');
